<?php

    namespace Admin\Controller;

    use Common\Controller\AdminbaseController;

    class AdminSaleManagerController extends AdminbaseController {

        protected $m_sale_manager;
        protected $m_recharge;
        protected $m_refund;

        public $status_label = array(
            '1' => '未审核',
            '2' => '审核通过',
        );

        function _initialize() {
            parent::_initialize();
            $this->m_sale_manager= D("Admin/SaleManager");
            $this->m_refund= D("Admin/Refund");
            $this->m_recharge= D("Admin/Recharge");
        }

        /**
         * 消费机构管理
         */
        public function index(){
            $this->assign("user_model",$this->user_model);
            $this->assign("is_manager",is_manager($this->user_model));

            $keyword = I("request.keyword");
            $this->assign("keyword",$keyword);
            if(!empty($keyword)){
                $where['sale_manage_name'] = array('like',"%$keyword%");
            }
            if (session('ADMIN_ID') == '10') {
                $count=$this->m_sale_manager->withTrashed()->where($where)->count();
            }else{
                $count=$this->m_sale_manager->where($where)->count();
            }
            $page = $this->page($count, 15);
            $list = $this->m_sale_manager->where($where)
                ->order("create_time desc")
                ->limit($page->firstRow , $page->listRows);
            // 如果是管理员可以看到删除的
            if (session('ADMIN_ID') == '10') {
                $list = $list->withTrashed()->order("delete_time ASC,create_time DESC")->select();
            }else{
                $list = $list->select();
            }
            // dump($this->m_sale_manager->getLastSql());

            $this->assign("page", $page->show('Admin'));
            $this->assign("list",$list);
            $this->display();
        }


        // 添加
        public function add(){
            $id = I("get.id",0,'intval');
            if(!empty($id)){
                $item=$this->m_sale_manager->where(array('id'=>$id))->find();
                $this->assign("item",$item);
            }

            $this->display();
        }

        // 提交
        public function add_post(){
            if (IS_POST) {
                $id = I("post.id");
                $insert = I("post.");

                if(empty($id)){
                    $result =$this->m_sale_manager->add($insert);
                }else{

                    $result = $this->m_sale_manager->where(array("id"=>$id))->save($insert);
                }
                if ($result) {
                    $this->success("添加成功！");
                } else {
                    $this->error("添加失败！");
                }

            }
        }

        // 删除
        public function delete(){
            if(isset($_GET['id'])){
                $id = I("get.id",0,'intval');
                if ($this->m_sale_manager->where(array('id'=>$id))->delete() !==false) {
                    $this->success("删除成功！");
                } else {
                    $this->error("删除失败！");
                }
            }else{
                $this->error("参数有误！");
            }

        }

        /*彻底删除就是把软删除关了，感觉理论上应该没错*/
        public function overdelete(){
            if(isset($_GET['id'])){
                $id = I("get.id",0,'intval');
                if ($this->m_sale_manager->delete($id,true)!==false) {
                    $this->success("删除成功！");
                } else {
                    $this->error("删除失败！");
                }
            }else{
                $this->error("参数有误！");
            }
        }

        /*彻底删除就是把软删除关了*/
        public function restore(){
            $id = I('get.id',0,'intval');

            $result = $this->m_sale_manager->restore($id);

            if ($result) {
                $this->success("数据已恢复！");
            }else{
                $this->success("数据恢复失败！");
            }
        }

        /**
         * 充值
         */
        public function recharge(){
            $id = I('get.id',0,'intval');
            if(empty($id)){
                $this->error("参数有误！");
            }
            $item=$this->m_sale_manager->where(array('id'=>$id))->find();
            $this->assign("item",$item);
            $this->display();
        }
        /**
         * 充值提交
         */
        public function recharge_post(){
            if (IS_POST) {
                $insert = I("post.");

                $result = $this->m_recharge->insert_data($insert,3);
                if ($result) {
                    $this->success("操作成功，已进入审核流程！");
                } else {
                    $this->error("操作失败！");
                }

            }
        }

        /**
         * 退款
         */
        public function refund(){
            $id = I('get.id',0,'intval');
            if(empty($id)){
                $this->error("参数有误！");
            }
            $item=$this->m_sale_manager->where(array('id'=>$id))->find();
            $this->assign("item",$item);
            $this->display();
        }
        /**
         * 退款提交
         */
        public function refund_post(){
            if (IS_POST) {
                $insert = I("post.");
                $result = $this->m_refund->insert_data($insert,1);
                if($result === -1){
                    $this->error("参数有误！");exit;
                }elseif($result === -2){
                    $this->error("金额输入有误！");exit;
                }elseif($result === -3){
                    $this->error("数据为空！");exit;
                }elseif($result === -4){
                    $this->error("操作失败！");exit;
                }elseif($result === -5){
                    $this->error("余额不足！");exit;
                }

                if ($result) {
                    $this->success("操作成功！");
                } else {
                    $this->error("操作失败！");
                }

            }
        }

        /**
         * 上级消费机构充值记录
         */
        public function recharge_bill(){
            $s_manager_id = I("request.s_manager_id");
            $this->assign("s_manager_id",$s_manager_id);
            if(!empty($s_manager_id)){
                $where['r.rid'] =$s_manager_id;
            }else{
                $where = array();
            }

            $where["r.model"] = 3;

            $s_start_time = I("s_start_time");
            $s_end_time = I("s_end_time");
            if(empty($s_start_time)){
                $s_start_time = strtotime(date("Y-m-d 00:00:00")." -7 day");
            }else{
                $s_start_time = strtotime($s_start_time.":00");
            }
            if(empty($s_end_time)){
                $s_end_time = time();
            }else{
                $s_end_time = strtotime($s_end_time.":59");
            }
            $this->assign("s_start_time",$s_start_time);
            $this->assign("s_end_time",$s_end_time);
            $where["r.create_time"]=array("BETWEEN",array($s_start_time,$s_end_time));


            $count=$this->m_recharge->alias("r")->where($where)->count();
            $page = $this->page($count, 15);



            if (I('s_download') != 1) {
                $list = $this->m_recharge->get_sale_manager_list($where, "r.create_time DESC", $page->firstRow, $page->listRows);
                foreach ($list as $_key => &$_row) {
                    $_row['status_label'] = $this->status_label[$_row['status']];
                }
            }else{
                $list = $this->m_recharge->get_sale_manager_list($where, "r.create_time DESC", $page->firstRow, $count);
                foreach ($list as $_key => &$_row) {
                    $_row['status_label'] = $this->status_label[$_row['status']];
                }
                $export_title = array(
                    'user_nicename' => '操作用户',
                    'user_phone' => '用户电话',
                    'sale_manage_name' => '上级消费机构名称',
                    'price' => '充值金额',
                    'after_balance' => '充值后余额',
                    'status_label' => '状态',
                    'create_time' => '创建时间',
                );

                $excel = new \Common\Lib\excel();
                $excel->export_data('上级消费机构充值记录', $export_title, $list);
                exit;
            }

            $sale_manager_list = $this->m_sale_manager->select();
            $this->assign("sale_manager_list",$sale_manager_list);
            $this->assign("page", $page->show('Admin'));
            $this->assign("list",$list);
            $this->display();
        }

        /**
         * 上级消费机构退款记录
         */
        public function refund_bill(){
            $s_manager_id = I("request.s_manager_id");
            $this->assign("s_manager_id",$s_manager_id);
            if(!empty($s_manager_id)){
                $where['r.rid'] =$s_manager_id;
            }else{
                $where = array();
            }

            $where["r.model"] = 1;

            $s_start_time = I("s_start_time");
            $s_end_time = I("s_end_time");
            if(empty($s_start_time)){
                $s_start_time = strtotime(date("Y-m-d 00:00:00")." -7 day");
            }else{
                $s_start_time = strtotime($s_start_time.":00");
            }
            if(empty($s_end_time)){
                $s_end_time = time();
            }else{
                $s_end_time = strtotime($s_end_time.":59");
            }
            $this->assign("s_start_time",$s_start_time);
            $this->assign("s_end_time",$s_end_time);
            $where["r.create_time"]=array("BETWEEN",array($s_start_time,$s_end_time));


            $count=$this->m_refund->alias("r")->where($where)->count();
            $page = $this->page($count, 15);
            if (I('s_download') != 1) {
                $list = $this->m_refund->get_sale_manager_list($where, "r.create_time DESC", $page->firstRow, $page->listRows);
            }else{
                $list = $this->m_refund->get_sale_manager_list($where, "r.create_time DESC", $page->firstRow, $count);
                $export_title = array(
                    'user_nicename' => '操作用户',
                    'user_phone' => '用户电话',
                    'sale_manage_name' => '上级消费机构名称',
                    'price' => '退款金额',
                    'after_balance' => '退款后余额',
                    'create_time' => '创建时间',
                );

                $excel = new \Common\Lib\excel();
                $excel->export_data('上级消费机构退款记录', $export_title, $list);
                exit;
            }

            $sale_manager_list = $this->m_sale_manager->select();
            $this->assign("sale_manager_list",$sale_manager_list);
            $this->assign("page", $page->show('Admin'));
            $this->assign("list",$list);
            $this->display();
        }

        /**
         * 上级消费实时余额
         */
        public function real_bill(){
            $where = array();
            //时间筛选
            $s_start_time = I("s_start_time");
            $s_end_time = I("s_end_time");
            if(empty($s_start_time)){
                $s_start_time = strtotime(date("Y-m-d 00:00:00")."-7 day");
            }else{
                $s_start_time = strtotime($s_start_time.":00");
            }
            if(empty($s_end_time)){
                $s_end_time = time();
            }else{
                $s_end_time = strtotime($s_end_time.":59");
            }
            $this->assign("s_start_time",$s_start_time);
            $this->assign("s_end_time",$s_end_time);
            $where["c.create_time"]=array("BETWEEN",array($s_start_time,$s_end_time));


            if(!empty($card_id_list)){
                $where["c.cid"] = array("in",$card_id_list);
            }elseif(!(is_manager($this->user_model))){
                $where["c.cid"] = "abcd";
            }

            $list = M("CardSale")->field("c.*,sm.sale_manage_name,sm.balance")->alias("c")
                ->join("bdt_sale_manager sm ON sm.id = c.sale_manager_id")
                ->where($where)
                ->order("c.create_time DESC")->select();
            $result = array();
            foreach($list AS $litem){
                $key = $litem['sale_manager_id'];
                if(empty($result[$key])){
                    $result[$key]['time'] = date("Y-m-d",$litem["create_time"]);
                    $result[$key]['sale_manage_name'] =$litem['sale_manage_name'];
                    $result[$key]['sale_num'] = 1;
                    if ($litem['is_oil'] == 1) {
                        $result[$key]['sale_oil_num'] = $litem['goods_num'];
                        $result[$key]['sale_nooil_num'] = 0;
                    }else{
                        $result[$key]['sale_oil_num'] = 0;
                        $result[$key]['sale_nooil_num'] = $litem['goods_num'];
                    }
                    $result[$key]['balance'] = $litem['balance'];
                }else{
                    $result[$key]['sale_num']++;
                    if ($litem['is_oil'] == 1) {
                        $result[$key]['sale_oil_num']+=$litem['goods_num'];
                    }else{
                        $result[$key]['sale_nooil_num']+=$litem['goods_num'];
                    }

                }
            }


            if(I("s_download") == 1){
                $export_title = array(
                    'sale_manage_name'=>'机构名称',
                    'balance'=>'机构余额',
                    'sale_num'=>'销售笔数',
                    'sale_oil_num'=>'加油数量（升）',
                    'sale_nooil_num'=>'加气数量（升）',
                );
                $excel = new \Common\Lib\excel();
                $excel->export_data('上级消费实时统计',$export_title,$result);exit;
            }

            $this->assign("list",$result);
            $this->display();
        }


    }